package com.evildog.workspace.system.application.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.evildog.workspace.common.object.PageQuery;
import com.evildog.workspace.common.result.PagedResult;
import com.evildog.workspace.dependencies.mybatis.object.QueryConditionDTO;
import com.evildog.workspace.dependencies.mybatis.util.QueryConditionUtils;
import com.evildog.workspace.system.application.bo.ApiPermissionCategoryPageBO;
import com.evildog.workspace.system.application.convert.ApiPermissionCategoryConvert;
import com.evildog.workspace.system.application.dto.ApiPermissionCategoryDTO;
import com.evildog.workspace.system.application.dto.ApiPermissionCategoryPageDTO;
import com.evildog.workspace.system.application.entity.ApiPermissionCategory;
import com.evildog.workspace.system.application.entity.ApiPermissionCategoryPageDO;
import com.evildog.workspace.system.application.mapper.ApiPermissionCategoryMapper;
import com.evildog.workspace.system.application.service.ApiPermissionCategoryService;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.List;

@Service
public class ApiPermissionCategoryServiceImpl extends ServiceImpl<ApiPermissionCategoryMapper, ApiPermissionCategory> implements ApiPermissionCategoryService {

    @Override
    public PagedResult<ApiPermissionCategoryPageBO> getPageList(PageQuery query, List<QueryConditionDTO> queryConditions, ApiPermissionCategoryPageDTO pageDTO) {
        IPage<ApiPermissionCategoryPageDO> page = this.baseMapper.getPageList(
                QueryConditionUtils.getPagination(query),
                queryConditions,
                pageDTO);
        return ApiPermissionCategoryConvert.INSTANCE.convert(page);
    }

    @Override
    public boolean save(ApiPermissionCategoryDTO dto){
        ApiPermissionCategory newApiPermissionCategory = ApiPermissionCategoryConvert.INSTANCE.convert(dto);
        return save(newApiPermissionCategory);
    }

    @Override
    public List<ApiPermissionCategoryPageBO> getValidList() {
        List<ApiPermissionCategory> list = list();
        return ApiPermissionCategoryConvert.INSTANCE.convertBOList(list);
    }

    @Override
    public boolean updateById(Long id, ApiPermissionCategoryDTO dto){
        ApiPermissionCategory oldApiPermissionCategory = getById(id);
        Assert.notNull(oldApiPermissionCategory, "apiPermissionCategory is null!");
        ApiPermissionCategory newApiPermissionCategory = ApiPermissionCategoryConvert.INSTANCE.convert(dto);
        newApiPermissionCategory.setId(id);
        return updateById(newApiPermissionCategory);
    }
}
